<?php

    class File
    {
        
        public static function save($file, $group, $name)
        {
            if (is_array($file) && ($file['name'] !== "") && ($file['name'] !== null) && file_exists($file['tmp_name']))
            {
                File::delete($group, $name);
                $x = explode('.', $file['name']);
                $ext = strtolower($x[count($x) - 1]);
                $destname = FILES . $group . '/' . $name . '.' . $ext;
                return move_uploaded_file($file['tmp_name'], $destname);
            }
            return false;
        }
        
        public static function delete($group, $name)
        {
            $link = File::getLink($group, $name);
            if ($link !== null)
                return unlink($link);
            else
                return false;
        }
        
        public static function getLink($group, $name)
        {
            $extensions = array('png', 'jpg', 'gif', 'zip', 'rar', 'wagame');
            foreach ($extensions as $ext)
            {
                $link = FILES . $group . '/' . $name . '.' . $ext;
                if (file_exists($link)) return $link;
            }
            return null;
        }
        
        public static function exists($group, $name)
        {
            return (File::getLink($group, $name) !== null);
        }
        
    }

?>
